﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class GameOver : MonoBehaviour
{
    public Text txt_Score, txt_BestScore, txt_diamondCount;
    public Button btn_Restart, btn_Rank, btn_Home;
    public Image Img_New;

    //public static GameOver Instance;
    private void Awake()
    {
       // Instance = this;
        //监听按钮点击事件
        btn_Restart.onClick.AddListener(OnRestartButtonClick);
        btn_Rank.onClick.AddListener(OnRankButtonClick);
        btn_Home.onClick.AddListener(OnHomeButtonClick);
        //监听页面显示
        EventCenter.AddListener(EventDefine.ShowGameOverPanel, Show);
        gameObject.SetActive(false);    
    }

    //public void OpenPanel() {
    //    Show();
    //}

    private void Show()
    {
        //当前成绩大于最高分
        if (GameManager.Instance.GetGameScore()>GameManager.Instance.GetBestScore())
        {//当前
            Img_New.gameObject.SetActive(true);
            txt_BestScore.text = "最高分  " + GameManager.Instance.GetGameScore();
        }
        else
        {
            Img_New.gameObject.SetActive(false);
            txt_BestScore.text = "最高分  " + GameManager.Instance.GetBestScore();
        }
        //存储最高分-替换
        GameManager.Instance.SaveScore(GameManager.Instance.GetGameScore());

        txt_Score.text = GameManager.Instance.GetGameScore().ToString();
        txt_diamondCount.text ="+"+ GameManager.Instance.getGameDiamond().ToString();
        //更新总的钻石数量
        GameManager.Instance.UpdateDiamond(GameManager.Instance.getGameDiamond());

        gameObject.SetActive(true);
    }
    /// <summary>
    /// 重新游戏的按钮点击
    /// </summary>
    private void OnRestartButtonClick()
    {
        //广播事件码--音效
        EventCenter.Broadcast(EventDefine.PlayClickAudio);
        SceneManager.LoadScene(SceneManager.GetActiveScene().name);//加载页面
        GameData.isAgainGame = true;
    }
    /// <summary>
    /// 排行榜按钮点击
    /// </summary>
    private void OnRankButtonClick()
    {
        //广播事件码--排行榜，音效
        EventCenter.Broadcast(EventDefine.ShowRankPanel);
        EventCenter.Broadcast(EventDefine.PlayClickAudio);
    }
    /// <summary>
     /// 点击回到主页
     /// </summary>
    private void OnHomeButtonClick()
    {
        //广播事件码--音效
        EventCenter.Broadcast(EventDefine.PlayClickAudio);
        SceneManager.LoadScene(SceneManager.GetActiveScene().name);//加载页面
        GameData.isAgainGame = false;
    }

    private void OnDestroy()
    {
        EventCenter.RemoveListener(EventDefine.ShowGameOverPanel,Show);
    }
}
